package cn.kgc.shiro02.controller;

import cn.kgc.shiro02.commons.CodeMsg;
import cn.kgc.shiro02.commons.ResponseResult;
import cn.kgc.shiro02.dto.UserFormDTO;
import cn.kgc.shiro02.dto.UserLoginDTO;
import cn.kgc.shiro02.dto.UserPageDTO;
import cn.kgc.shiro02.entity.User;
import cn.kgc.shiro02.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;


/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/1
 * @Description:
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("查询用户信息列表")
    @GetMapping("all")
    public ResponseResult selectPage(UserPageDTO userPageDTO){

     return userService.selectPage(userPageDTO);
    }

    @RequiresRoles({"root"})
    @RequiresPermissions({"user:add"})
    @PostMapping("add")
    @ApiOperation("添加用户")
    public ResponseResult addUser(@RequestBody UserFormDTO userFormDTO){
        return userService.saveUser(userFormDTO);
    }


    @ApiOperation("用户登录")
    @GetMapping("login")
    public ResponseResult login(UserLoginDTO userLoginDTO, HttpSession session){
        // 1.判定用户的验证码是否正确
        String code = (String) redisTemplate.opsForValue().get(session.getId());
        String captcha = userLoginDTO.getCaptcha();
        if(code==null){ //  验证码失效
            return  new ResponseResult(CodeMsg.CAPTCHA_EXPIRE);
        }else{
            if (code.equals(captcha)){ // 验证码正确
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userLoginDTO.getUsername(), userLoginDTO.getPassword());
                // 判定用户是否开启 免登录
                String rememberMe = userLoginDTO.getRememberMe();
                if("true".equals(rememberMe)){
                    usernamePasswordToken.setRememberMe(true);
                }
                subject.login(usernamePasswordToken);
                return  new ResponseResult(CodeMsg.SUCCESS);
            }else {  // 验证码错误
                return  new ResponseResult(CodeMsg.CAPTCHA_ERROR);
            }
        }

    }

    @ApiOperation("用户退出")
    @GetMapping("logout")
    public ResponseResult  logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return  new ResponseResult(CodeMsg.SUCCESS);
    }

    @ApiOperation("删除用户")
    @RequiresRoles({"root"})
    @RequiresPermissions({"user:delete"})
    @DeleteMapping("delete")
    public ResponseResult  delete(Integer[] ids){
        return  userService.deleteMore(ids);
    }


    @ApiOperation("更新用户状态")
    @PostMapping("state")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "Integer",required = true),
            @ApiImplicitParam(name = "state",value = "用户状态,用户是否被禁用",dataType = "Integer",required = true)
    })
    public ResponseResult  updateUserState(Integer id,Integer state){
        // TODO  调用业务层 更新用户信息
        User user = new User();
        user.setId(id);
        user.setState(state);
        userService.updateById(user);
        return new ResponseResult(CodeMsg.SUCCESS);
    }


    @ApiOperation("根据id获取用户信息")
    @GetMapping("getById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "Integer",required = true)
    })
    public  ResponseResult  getById(Integer id){
       return   userService.selectById(id);
    }

    @ApiOperation("更新用户信息")
    @RequiresRoles({"root"})
    @RequiresPermissions({"user:update"})
    @PutMapping("update")
    public ResponseResult  update(@RequestBody UserFormDTO userFormDTO ){
        // TODO  调用业务层 更新用户信息
        System.out.println("=====userFormDTO===== " + userFormDTO);
        return  userService.updateUser(userFormDTO);
    }

    @ApiOperation("动态加载菜单")
    @GetMapping("loadMenu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",required = true)
    })
    public Map<String,Object>  loadMenu(String username){

       return    userService.loadMenu(username);

    }
}
